import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:s_k_todo/model/task_model.dart';

import '../data/base_data.dart';
import '../data/dio_static.dart';

class TaskAddProvider extends ChangeNotifier {
  late final TaskApi _taskApi = TaskApi(DioStatic.getInstance().dio);
  late final stepList = <String>[];
  bool _addStep = false;

  String _taskName = "";
  String _taskTip = "";

  Future<TaskEntity?> addTask() async {
    TaskEntity entity = TaskEntity();
    entity
      ..taskTitle = _taskName
      ..tips = _taskTip
      ..stepList = stepList;
    BaseData<TaskEntity> data = await _taskApi.addTask(entity);
    if (data.code == 0) {
      return data.data;
    } else {
      throw Exception(data.message ?? "添加任务失败");
    }
  }

  void addStepName(String stepName) {
    stepList.add(stepName);
    _addStep = !_addStep;
    notifyListeners();
  }

  set taskName(String value) {
    _taskName = value;
    if (kDebugMode) {
      print("taskName: $value");
    }
  }

  set taskTip(String value) {
    _taskTip = value;
    if (kDebugMode) {
      print("taskTip: $value");
    }
  }

  bool get addStep => _addStep;

  set addStep(bool value) {
    _addStep = value;
    notifyListeners();
  }

  void removeStepByName(String e) {
    stepList.remove(e);
    notifyListeners();
  }
}
